﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using RM2012Server.Business;

namespace RM2012Server.Model
{
    public class UserDAO
    {
        DataServices m_UserDAO = new DataServices();

        public DataTable GetAll()
        {
            m_UserDAO.Load("GetAllUsers", null);
            return m_UserDAO;
        }

        public DataTable GetById(String userId)
        {
            SqlParameter param = new SqlParameter("@UserId", SqlDbType.VarChar, 6);
            param.Value = userId;

            m_UserDAO.Load("GetUserById", param);
            return m_UserDAO;
        }

        public DataTable GetByUsername(String userName)
        {
            SqlParameter param = new SqlParameter("@UserName", SqlDbType.VarChar, 50);
            param.Value = userName;

            m_UserDAO.Load("GetUserByUserName", param);
            return m_UserDAO;
        }

        public DataTable GetByFullName(String fullName)
        {
            SqlParameter param = new SqlParameter("@FullName", SqlDbType.NVarChar, 100);
            param.Value = fullName;

            m_UserDAO.Load("GetUserByFullName", param);
            return m_UserDAO;
        }

        public DataTable GetByAddress(String address)
        {
            SqlParameter param = new SqlParameter("@Address", SqlDbType.NVarChar, 200);
            param.Value = address;

            m_UserDAO.Load("GetUserByAddress", param);
            return m_UserDAO;
        }

        public DataTable GetByPhone(String phone)
        {
            SqlParameter param = new SqlParameter("@Phone", SqlDbType.NVarChar, 100);
            param.Value = phone;

            m_UserDAO.Load("GetUserByPhone", param);
            return m_UserDAO;
        }

        public DataTable GetByEmail(String email)
        {
            SqlParameter param = new SqlParameter("@Email", SqlDbType.NVarChar, 100);
            param.Value = email;

            m_UserDAO.Load("GetUserByEmail", param);
            return m_UserDAO;
        }

        public DataTable GetByGroup(String userGroupId)
        {
            SqlParameter param = new SqlParameter("@UserGroupId", SqlDbType.VarChar, 4);
            param.Value = userGroupId;

            m_UserDAO.Load("GetUserByGroup", param);
            return m_UserDAO;
        }

        public bool UserHasGroup(String userId)
        {
            SqlParameter param = new SqlParameter("@UserId", SqlDbType.VarChar, 6);
            param.Value = userId;

            int result = Convert.ToInt32(m_UserDAO.ExecuteScalar("UserHasGroup", param));
            if (result > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool ExistUserByUserName(String userName)
        {
            SqlParameter param = new SqlParameter("@UserName", SqlDbType.VarChar, 50);
            param.Value = userName;

            int result = Convert.ToInt32(m_UserDAO.ExecuteScalar("ExistUserByUserName", param));
            if (result > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        public bool ExistUserByUserNameAndPassword(String userName, String passWord)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@UserName", SqlDbType.VarChar, 50);
            param[0].Value = userName;

            param[1] = new SqlParameter("@PassWord", SqlDbType.VarChar, 50);
            param[1].Value = passWord;

            int result = Convert.ToInt32(m_UserDAO.ExecuteScalar("ExistUserByUserNameAndPassword", param));
            if (result > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        public DataTable Wellcome(String userId)
        {
            SqlParameter param = new SqlParameter("@UserId", SqlDbType.VarChar, 50);
            param.Value = userId;

            m_UserDAO.Load("Wellcome", param);
            return m_UserDAO;
        }


        public DataTable GetUserGroup(String userId)
        {
            SqlParameter param = new SqlParameter("@UserId", SqlDbType.VarChar, 50);
            param.Value = userId;

            m_UserDAO.Load("GetUserGroup", param);
            return m_UserDAO;
        }


        public int Add(UserInfo info)
        {
            SqlParameter[] param = new SqlParameter[10];
            param[0] = new SqlParameter("@UserId", SqlDbType.VarChar, 6);
            param[0].Value = info.UserId;

            param[1] = new SqlParameter("@UserName", SqlDbType.VarChar, 50);
            param[1].Value = info.UserName;

            param[2] = new SqlParameter("@PassWord", SqlDbType.VarChar, 50);
            param[2].Value = info.PassWord;

            param[3] = new SqlParameter("@FullName", SqlDbType.NVarChar, 100);
            param[3].Value = info.FullName;

            param[4] = new SqlParameter("@Salary", SqlDbType.Real);
            param[4].Value = info.Salary;

            param[5] = new SqlParameter("@Address", SqlDbType.NVarChar, 200);
            param[5].Value = info.Address;

            param[6] = new SqlParameter("@Phone", SqlDbType.NVarChar, 100);
            param[6].Value = info.Phone;

            param[7] = new SqlParameter("@Email", SqlDbType.NVarChar, 100);
            param[7].Value = info.Email;

            param[8] = new SqlParameter("@Sex", SqlDbType.Bit);
            param[8].Value = info.Sex;

            param[9] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[9].Value = info.Remark;

            return m_UserDAO.ExecuteUpdate("AddUser", param);
        }


        public int Update(UserInfo info)
        {
            SqlParameter[] param = new SqlParameter[10];
            param[0] = new SqlParameter("@UserId", SqlDbType.VarChar, 6);
            param[0].Value = info.UserId;

            param[1] = new SqlParameter("@UserName", SqlDbType.VarChar, 50);
            param[1].Value = info.UserName;

            param[2] = new SqlParameter("@PassWord", SqlDbType.VarChar, 50);
            param[2].Value = info.PassWord;

            param[3] = new SqlParameter("@FullName", SqlDbType.NVarChar, 100);
            param[3].Value = info.FullName;

            param[4] = new SqlParameter("@Salary", SqlDbType.Real);
            param[4].Value = info.Salary;

            param[5] = new SqlParameter("@Address", SqlDbType.NVarChar, 200);
            param[5].Value = info.Address;

            param[6] = new SqlParameter("@Phone", SqlDbType.NVarChar, 100);
            param[6].Value = info.Phone;

            param[7] = new SqlParameter("@Email", SqlDbType.NVarChar, 100);
            param[7].Value = info.Email;

            param[8] = new SqlParameter("@Sex", SqlDbType.Bit);
            param[8].Value = info.Sex;

            param[9] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[9].Value = info.Remark;

            return m_UserDAO.ExecuteUpdate("UpdateUser", param);
        }

        public int UpdateNoPass(UserInfo info)
        {
            SqlParameter[] param = new SqlParameter[9];
            param[0] = new SqlParameter("@UserId", SqlDbType.VarChar, 6);
            param[0].Value = info.UserId;

            param[1] = new SqlParameter("@UserName", SqlDbType.VarChar, 50);
            param[1].Value = info.UserName;

            param[2] = new SqlParameter("@FullName", SqlDbType.NVarChar, 100);
            param[2].Value = info.FullName;

            param[3] = new SqlParameter("@Salary", SqlDbType.Real);
            param[3].Value = info.Salary;

            param[4] = new SqlParameter("@Address", SqlDbType.NVarChar, 200);
            param[4].Value = info.Address;

            param[5] = new SqlParameter("@Phone", SqlDbType.NVarChar, 100);
            param[5].Value = info.Phone;

            param[6] = new SqlParameter("@Email", SqlDbType.NVarChar, 100);
            param[6].Value = info.Email;

            param[7] = new SqlParameter("@Sex", SqlDbType.Bit);
            param[7].Value = info.Sex;

            param[8] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[8].Value = info.Remark;

            return m_UserDAO.ExecuteUpdate("UpdateUserNoPass", param);
        }

        public int UpdateProfile(UserInfo info)
        {
            SqlParameter[] param = new SqlParameter[5];
            param[0] = new SqlParameter("@UserId", SqlDbType.VarChar, 6);
            param[0].Value = info.UserId;

            param[1] = new SqlParameter("@FullName", SqlDbType.NVarChar, 100);
            param[1].Value = info.FullName;

            param[2] = new SqlParameter("@Address", SqlDbType.NVarChar, 200);
            param[2].Value = info.Address;

            param[3] = new SqlParameter("@Phone", SqlDbType.NVarChar, 100);
            param[3].Value = info.Phone;

            param[4] = new SqlParameter("@Email", SqlDbType.NVarChar, 100);
            param[4].Value = info.Email;

            return m_UserDAO.ExecuteUpdate("UpdateProfile", param);
        }

        public int ChangePassword(UserInfo info)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@UserId", SqlDbType.VarChar, 6);
            param[0].Value = info.UserId;

            param[1] = new SqlParameter("@PassWord", SqlDbType.VarChar, 50);
            param[1].Value = info.PassWord;

            return m_UserDAO.ExecuteUpdate("ChangePassword", param);
        }

        public object GetIdByUsername(String userName)
        {
            SqlParameter param = new SqlParameter("@UserName", SqlDbType.VarChar, 50);
            param.Value = userName;

            return m_UserDAO.ExecuteScalar("GetUserIdByUsername", param);
        }

        public int Delete(String userId)
        {
            SqlParameter param = new SqlParameter("@UserId", SqlDbType.VarChar, 6);
            param.Value = userId;

            return m_UserDAO.ExecuteNoneQuery("DeleteUser", param);
        }

        public DataRow AddNewRow()
        {
            return m_UserDAO.NewRow();
        }

        public void AddNewUser(DataRow m_Row)
        {
            m_UserDAO.Rows.Add(m_Row);
        }

        public bool SaveData()
        {
            return m_UserDAO.ExecuteNoneQuery() > 0;
        }
    }
}
